package at.htlkaindorf.jsf.view;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import at.htlkaindorf.jsf.application.DataBean;
import at.htlkaindorf.jsf.domain.Entry;
import at.htlkaindorf.jsf.session.SessionBean;

@ManagedBean(name = "editBean")
@SessionScoped
public class EditBean {

    @ManagedProperty(value = "#{sessionBean}")
    private SessionBean sessionBean;

    @ManagedProperty(value = "#{dataBean}")
    private DataBean dataBean;

    private String name, email, title, text;

    private Long id;

    @PostConstruct
    public void init() {
        if (sessionBean.getUsername() == null) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("guestbookLogin.xhtml");
            } catch (IOException e) {
                // TODO: Logging of Java IOException.
            }
        }
        name = sessionBean.getUsername();
    }

    public String edit(long id) {

        this.id = id;
        List<Entry> entries = dataBean.getGuestbookEntries();
        Entry myEntry=null;
        for (Entry entry : entries) {
            if (entry.getId() == id) {
                myEntry = entry;
            }
        }

        name = myEntry.getName();
        email = myEntry.getEmail();
        title = myEntry.getTitle();
        text = myEntry.getText();
        if (!myEntry.getName().equals(sessionBean.getUsername()) || sessionBean.getUsername().equals("admin")) {
            return "guestbookOverview";
        }
        System.out.print(this.id);
        return "guestbookEditEntry";
    }

    public String saveChanges() {
        List<Entry> entries = dataBean.getGuestbookEntries();
        for (Entry entry : entries) {
            if (entry.getId() == id) {
                entry.setName(name);
                entry.setEmail(email);
                entry.setPostDate(new Date());
                entry.setText(text);
                entry.setTitle(title);
            }
        }
        System.out.print(this.id);
        return "guestbookOverview";
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getEmail() {
        return email;
    }


    public void setEmail(String email) {
        this.email = email;
    }


    public String getTitle() {
        return title;
    }


    public void setTitle(String title) {
        this.title = title;
    }


    public String getText() {
        return text;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }


    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public void setText(String text) {
        this.text = text;
    }


    public DataBean getDataBean() {
        return dataBean;
    }


    public void setDataBean(DataBean dataBean) {
        this.dataBean = dataBean;
    }


}
